package com.sina.shop.api;

import com.sina.shop.domain.collect.CollectGoods;
import com.sina.shop.service.collect.CollectGoodsService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-10:38
 */
@RestController
@RequestMapping("/collectGoods")
public class CollectGoodsController extends BaseController {

    @Autowired
    @Qualifier("collectGoodsService")
    private CollectGoodsService collectGoodsService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getCollectGoodsListByAdminId", method = RequestMethod.GET)
    public List<CollectGoods> getCollectGoodsListByAdminId(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCollectGoodsListByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectGoodsService.getCollectGoodsListByAdminId(adminId);
    }

    @RequestMapping(value = "/getCollectGoodsByAidAndBid", method = RequestMethod.GET)
    public CollectGoods getCollectGoodsByAidAndBid(Long adminId, Long businessId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCollectGoodsByAidAndBid, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectGoodsService.getCollectGoodsByAidAndBid(adminId, businessId);
    }

    @RequestMapping(value = "/deleteCollectGoods", method = RequestMethod.POST)
    public Integer deleteCollectGoods(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteCollectGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectGoodsService.deleteCollectGoods(id);
    }

    @RequestMapping(value = "/saveCollectGoods", method = RequestMethod.POST)
    public CollectGoods saveCollectGoods(CollectGoods collectGoods) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveCollectGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectGoodsService.saveCollectGoods(collectGoods);
    }
}
